package com.mikey.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mikey.common.PageResult;
import com.mikey.common.PwdEntity;
import com.mikey.common.Result;
import com.mikey.pojo.TbSeller;
import com.mikey.service.SellerService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户controller
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/seller")
public class SellerController {

    @Reference
    private SellerService sellerService;

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<TbSeller> findAll() {
        return sellerService.findAll();
    }


    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(int page, int rows) {
        return sellerService.findPage(page, rows);
    }

    /**
     * 增加
     *
     * @param seller
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody TbSeller seller) {
        try {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            seller.setPassword(passwordEncoder.encode(seller.getPassword()));
            sellerService.add(seller);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @param seller
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody TbSeller seller) {
        try {
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            seller.setSellerId(name);
            sellerService.update(seller);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @param
     * @return
     */
    @RequestMapping("/findOne")
    public TbSeller findOne() {
        String sellerId = SecurityContextHolder.getContext().getAuthentication().getName();
        TbSeller seller = sellerService.findOne(sellerId);
        return seller;
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(String[] sellerIds) {
        try {
            sellerService.delete(sellerIds);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    /**
     * 查询+分页
     *
     * @param
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody TbSeller seller, int page, int rows) {
        return sellerService.findPage(seller, page, rows);
    }

    @RequestMapping("/name")
    public Map name() {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        Map map = new HashMap();
        map.put("loginName", name);
        return map;
    }

    //修改密码
    @RequestMapping("/updatePwd")
    public Result updatePwd(@RequestBody PwdEntity pwdEntity) {
        String id = SecurityContextHolder.getContext().getAuthentication().getName();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        if (passwordEncoder.matches(pwdEntity.getOldPwd(), sellerService.findOne(id).getPassword())) {
            pwdEntity.setNewPwd(passwordEncoder.encode(pwdEntity.getNewPwd()));
            if (sellerService.updatePwd(pwdEntity, id)) {
                return new Result(true, "修改成功");
            }
        }
        return new Result(false, "修改失败");
    }
}
